
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @desc: datagather-tools
 * @name: IFengNewsCrawler.java
 * @author: tompai
 * @createTime: 2018年5月2日 下午5:18:06
 * @history:
 * @version: v1.0
 */

public class FengNewsCrawlerDemo {

	private static final Logger logger = Logger.getLogger(FengNewsCrawlerDemo.class);

	/**
	 * @author: tompai
	 * @createTime: 2018年5月2日 下午5:18:06
	 * @history:
	 * @param args
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String ifengUrl = "http://news.ifeng.com/listpage/7130/1/list.shtml";
		Document doc;
		try {
			doc = Jsoup.connect(ifengUrl)
					.userAgent("Mozilla")
					.cookie("auth", "token")
					.timeout(3000)
					.get();

			String title = doc.title();

			logger.info("Title:" + title);

			// Element content = doc.getElementsByClass("comListCon");
			Elements links = doc.getElementsByClass("comListBox");

			for (Element link : links) {

				String linkHref = link.getElementsByTag("a").attr("href");
				String linkText = link.getElementsByTag("a").text();
				String linkTime = link.getElementsByTag("p").text();
				
				logger.info("linkHref:" + linkHref);
				logger.info("linkText:" + linkText);
				logger.info("linkText:" + linkTime);
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
